package com.yanqu.road.server.cmd.activity.suitang;

import com.yanqu.road.entity.activity.suitang.config.SuiTangConfigSync;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.suitang.SuiTang;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.DecorationModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.suitang.SuiTangMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

@Cmd(code = GameProtocol.S_SUI_TANG_GET_DATA_FOR_OB, desc = "进入游戏界面获取游戏数据")
public class SuiTangGetDataForOBCmd extends AbstractCommand {
    private SuiTang.SuiTangGetDataForObReqMsg reqMsg;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        reqMsg = SuiTang.SuiTangGetDataForObReqMsg.parseFrom(packet.getBytes());
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        SuiTangConfigSync config = SuiTangMgr.getConfig();
        if (config == null || !ActivityMgr.activityInShowTime(config.getActivityInfo())) {
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }

        if (!config.isRequestTime()) {
            ret = GameErrorCode.E_SUI_TANG_NO_OPERATE_TIME;
            return;
        }

        SuiTang.CrossSuiTangGetDataForObReqMsg.Builder builder = SuiTang.CrossSuiTangGetDataForObReqMsg.newBuilder();
        builder.setActivityId(config.getActivityInfo().getActivityId());
        builder.setReq(reqMsg);
        player.sendUnionActivityGroupPacket(CrossProtocol.C2_SUI_TANG_GET_DATA_FOR_OB, builder, config.getActivityInfo().getActivityId());
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret == 0) {
            return;
        }

        SuiTang.SuiTangGetDataRespMsg.Builder respMsg = SuiTang.SuiTangGetDataRespMsg.newBuilder();
        respMsg.setRet(ret);
        player.sendPacket(ClientProtocol.U_SUI_TANG_GET_DATA_FOR_OB, respMsg);
    }
}
